#include "app.h"
#include <uis.h>
#include "../dbg/dbg.h"

// TODO: set previous state here
#define BACK_STATE			NEWAPP_STATE_MAIN


// -----------------------------------------------------------------------------------------
// Local constants

static const WCHAR		str_hw[] = {'H','e','l','l','o',' ','W','o','r','l','d','!',0};


// -----------------------------------------------------------------------------------------
// Local functions

static UINT32 HandleDone( EVENT_STACK_T * ev_st, void * app );
static UINT32 HandleCancel( EVENT_STACK_T * ev_st, void * app );


// -----------------------------------------------------------------------------------------
// Global handlers array

const EVENT_HANDLER_ENTRY_T notice_state_handlers[] =
{
	{ EV_DIALOG_DONE,			HandleCancel },		// Pressing RED key
   	{ EV_DONE,					HandleDone },		// Pressing "back" key

	{ STATE_HANDLERS_END,		NULL }
};

// -----------------------------------------------------------------------------------------
// Global enter/exit state functions

UINT32 NoticeStateEnter( EVENT_STACK_T * ev_st, void * app, ENTER_STATE_TYPE_T type )
{
	NEWAPP_T *		papp = newapp(app);
	SU_PORT_T		port = papp->apt.port;
	UIS_DIALOG_T	dialog;
	CONTENT_T		cnt;
	UINT32			status;
	
	dbg("NoticeStateEnter: Enter, type = %d", type);
	if ( type != ENTER_STATE_ENTER )
		return RESULT_OK;
	
	// TODO: add your code here
	
	status = UIS_MakeContentFromString( "q0", &cnt, str_hw );
	dbg("NoticeStateEnter: UIS_MakeContentFromString DONE, status = %d", status);
	
	if ( status != RESULT_OK )
		return status;
	
	dialog = UIS_CreateNotice( &port,
								&cnt,
								10*1000, // in msec
								NOTICE_TYPE_DEFAULT,
								FALSE,
								NULL );
	dbg("NoticeStateEnter: UIS_CreateNotice DONE, dialog = %d", dialog);
	
	if ( dialog == 0 )
		return RESULT_FAIL;
	papp->apt.dialog = dialog;
	
	dbg("NoticeStateEnter: END", 0);
	return RESULT_OK;
}

UINT32 NoticeStateExit( EVENT_STACK_T * ev_st, void * app, EXIT_STATE_TYPE_T type )
{
	NEWAPP_T *		papp = newapp(app);
	UINT32			status;
	
	dbg("NoticeStateExit: Enter, type = %d", type);
	if ( type != EXIT_STATE_EXIT )
		return RESULT_OK;
	
	status = APP_UtilUISDialogDelete( &papp->apt.dialog );
	
	dbg("NoticeStateExit: END, status = %d", status);
	return status;
}


// -----------------------------------------------------------------------------------------
// Other local functions

UINT32 HandleDone( EVENT_STACK_T * ev_st, void * app )
{
	NEWAPP_T *		papp = newapp(app);
	UINT32			status;
	
	dbg("HandleDone: Enter", 0);
	APP_ConsumeEv(ev_st, app);
	
	// Pressing "back" key
	// TODO: add your code here
	
	status = APP_UtilChangeState( BACK_STATE, ev_st, app );
	dbg("HandleDone: END, status = %d", status);
	return status;
}

UINT32 HandleCancel( EVENT_STACK_T * ev_st, void * app )
{
	dbg("HandleCancel: Enter", 0);
	APP_ConsumeEv(ev_st, app);
	
	return ELF_Exit( ev_st, app );
}


